// pages/order/confirm_order/confirm_order.js
var util=require('../../../utils/util');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        cart : [],
        totalMoney : 0,
        userInfo : 0,
        userNickName : "",
        userImg : "",
        restaurant_id : "",
        StoreName : "",
        table_id : "001",
        Time : '',
        user_openid: ""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.data.totalMoney = wx.getStorageSync('totalmoney');
        this.data.cart = wx.getStorageSync('goodcart');
        this.data.StoreName=wx.getStorageSync('CanteenName');
        this.data.restaurant_id=wx.getStorageSync('CanteenId');
        console.log(this.data.cart);
        console.log(this.data.totalMoney);
        this.setData({
            cart : this.data.cart,
            totalMoney : this.data.totalMoney,
            StoreName : this.data.StoreName
        })
        this.data.userInfo = wx.getStorageSync('userInfo');
        this.data.userNickName = this.data.userInfo.nickName;
        this.data.userImg = this.data.userInfo.avatarUrl;
        this.data.user_openid = wx.getStorageSync('openid');
        console.log("用户openid为:"+this.data.user_openid);
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    getTime:function(e){
        var that = this;
        var currentTime = util.formatTime(new Date());
        this.data.Time = currentTime;
        console.log(this.data.Time);
      },


    sub(){
        console.log(this.data.cart);
        console.log(this.data.user_openid);
        var that = this;
        setInterval(function(){
            that.setData({
                Time: util.formatTime(new Date())
            });    
        },1000);  
        this.getTime();


        //凯哥看这里凯哥看这里凯哥看这里凯哥看这里凯哥看这里凯哥看这里凯哥看这里凯哥看这里凯哥看这里凯哥看这里凯哥看这里凯哥看这里凯哥看这里凯哥看这里凯哥看这里凯哥看这里凯哥看这里凯哥看这里凯哥看这里
        //传值到后端
        wx.request({
          url: 'http://localhost:8080/getOrder',
          data:{
            //dish_id、dish_price和dish_num在这个cart里面
            "cart" : this.data.cart,
            "sumPrice" : this.data.totalMoney,
            "user_id" : this.data.user_openid,
            "userImg" : this.data.userImg,
            "restaurant_id" : this.data.restaurant_id,
            "table_id" : this.data.table_id,
            "seatTime" : this.data.Time
          },
          header: {
            "Content-Type": "application/json"
          },
          method: 'POST',
          success: function (res) {
            console.log(res)
            that.setData({       //this.setData的方法用于把传递过来的id转化成小程序模板语言
            //data: res.data    //拿到该商品详情对应的商品id，再调用getProdutInfo方法
        
          })
          wx.showToast({
            title: '付款成功',
            icon: 'success',
            duration: 2000,    //提示的延迟时间
            success:function(){
              setTimeout(function(){
                wx.switchTab({
                  url: '../../list/list',
                })
              },1000);
            }
          });
        },
        fail: function () {
          // fail
          wx.showToast({
            title: '提交失败',
            icon: 'error',
            duration: 2000    //提示的延迟时间
          });
        },
        complete: function () {
          // complete
        }
        })
    }

})